using Microsoft.AspNetCore.Mvc;
using MediPlatform.Write.Api.Services;
using MediPlatform.Write.Api.Models;
using MediPlatform.ErrorCode;

namespace MediPlatform.Write.Api.Controllers
{
    /// <summary>
    /// 退款申请测试控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class TestRefundController : ControllerBase
    {
        private readonly RefundApplicationWriteService _refundApplicationWriteService;
        private readonly ILogger<TestRefundController> _logger;

        public TestRefundController(
            RefundApplicationWriteService refundApplicationWriteService,
            ILogger<TestRefundController> logger)
        {
            _refundApplicationWriteService = refundApplicationWriteService;
            _logger = logger;
        }

        /// <summary>
        /// 测试确认退款
        /// </summary>
        /// <param name="id">退款申请ID</param>
        /// <returns>测试结果</returns>
        [HttpPost("confirm/{id}")]
        public async Task<ApiResult<object>> TestConfirmRefund(long id)
        {
            try
            {
                var request = new ConfirmRefundRequest
                {
                    Id = id,
                    Remark = "测试确认退款",
                    OperatorId = 1
                };

                var result = await _refundApplicationWriteService.ConfirmRefundAsync(request);
                
                if (result)
                {
                    return ApiResult<object>.Success("测试确认退款成功");
                }
                else
                {
                    return ApiResult<object>.Error("测试确认退款失败");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "测试确认退款失败，ID：{Id}", id);
                return ApiResult<object>.Error("测试确认退款失败：" + ex.Message);
            }
        }

        /// <summary>
        /// 测试拒绝退款
        /// </summary>
        /// <param name="id">退款申请ID</param>
        /// <returns>测试结果</returns>
        [HttpPost("reject/{id}")]
        public async Task<ApiResult<object>> TestRejectRefund(long id)
        {
            try
            {
                var request = new RejectRefundRequest
                {
                    Id = id,
                    Remark = "测试拒绝退款",
                    OperatorId = 1
                };

                var result = await _refundApplicationWriteService.RejectRefundAsync(request);
                
                if (result)
                {
                    return ApiResult<object>.Success("测试拒绝退款成功");
                }
                else
                {
                    return ApiResult<object>.Error("测试拒绝退款失败");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "测试拒绝退款失败，ID：{Id}", id);
                return ApiResult<object>.Error("测试拒绝退款失败：" + ex.Message);
            }
        }
    }
} 